/**
 * @file lv_port_disp_templ.c
 *
 */

 /*Copy this file as "lv_port_disp.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_disp.h"
#include "ft_lcd_ui.h"
#include "ft_lcd_drv.h"
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/
#define  MY_DISP_HOR_RES  (240)  //屏幕宽度
#define  MY_DISP_VER_RES  (320)  //屏幕高度
/**********************
 *  STATIC PROTOTYPES
 **********************/
static void disp_init(void);

static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p);
//static void gpu_fill(lv_disp_drv_t * disp_drv, lv_color_t * dest_buf, lv_coord_t dest_width,
//        const lv_area_t * fill_area, lv_color_t color);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_disp_init(void)
{
    /*-------------------------
     * Initialize your display
     * 初始化显示设备
     * -----------------------*/
    disp_init();

    /*-----------------------------
     * Create a buffer for drawing
     * 创建一个绘图缓冲区
     *----------------------------*/

    /**
     * LVGL requires a buffer where it internally draws the widgets.
     * LVGL需要一个缓冲区用来绘制小部件
     * Later this buffer will passed to your display driver's `flush_cb` to copy its content to your display.
     * 随后，这个缓冲区的内容会通过显示设备的flush_cb（显示设备刷新函数）复制到显示设备上
     * The buffer has to be greater than 1 display row
     * 这个缓冲区的大小需要大于显示设备一行的大小
     * 
     * There are 3 buffering configurations:
     * 1. Create ONE buffer:
     *      LVGL will draw the display's content here and writes it to your display
     *
     * 2. Create TWO buffer:
     *      LVGL will draw the display's content to a buffer and writes it your display.
     *      You should use DMA to write the buffer's content to the display.
     *      It will enable LVGL to draw the next part of the screen to the other buffer while
     *      the data is being sent form the first buffer. It makes rendering and flushing parallel.
     *
     * 3. Double buffering
     *      Set 2 screens sized buffers and set disp_drv.full_refresh = 1.
     *      This way LVGL will always provide the whole rendered screen in `flush_cb`
     *      and you only need to change the frame buffer's address.
     * 这里有 3 中缓冲配置:
        * 1. 单缓冲区:
        * LVGL 会将显示设备的内容绘制到这里，并将他写入显示设备。
        *
        * 2. 双缓冲区:
        * LVGL 会将显示设备的内容绘制到其中一个缓冲区，并将他写入显示设备。
        * 需要使用 DMA 将要显示在显示设备的内容写入缓冲区。
        * 当数据从第一个缓冲区发送时，它将使 LVGL 能够将屏幕的下一部分绘制到另一个缓冲区。
        * 这样使得渲染和刷新可以并行执行。
        *
        * 3. 全尺寸双缓冲区
        * 设置两个屏幕大小的全尺寸缓冲区，并且设置 disp_drv.full_refresh = 1。
        * LVGL 将始终以 'flush_cb' 的形式提供整个渲染屏幕，只需更改帧缓冲区的地址。
    */

    /* Example for 1) */
    static lv_disp_draw_buf_t draw_buf_dsc_1;
    //设置缓冲区的大小为10行屏幕的大小
    static lv_color_t buf_1[MY_DISP_HOR_RES * 10];                          /*A buffer for 10 rows*/
    //初始化显示缓冲区
    lv_disp_draw_buf_init(&draw_buf_dsc_1, buf_1, NULL, MY_DISP_HOR_RES * 10);   /*Initialize the display buffer*/

    // /* Example for 2) */
    // static lv_disp_draw_buf_t draw_buf_dsc_2;
    // static lv_color_t buf_2_1[MY_DISP_HOR_RES * 10];                        /*A buffer for 10 rows*/
    // static lv_color_t buf_2_2[MY_DISP_HOR_RES * 10];                        /*An other buffer for 10 rows*/
    // lv_disp_draw_buf_init(&draw_buf_dsc_2, buf_2_1, buf_2_2, MY_DISP_HOR_RES * 10);   /*Initialize the display buffer*/

    // /* Example for 3) also set disp_drv.full_refresh = 1 below*/
    // static lv_disp_draw_buf_t draw_buf_dsc_3;
    // static lv_color_t buf_3_1[MY_DISP_HOR_RES * MY_DISP_VER_RES];            /*A screen sized buffer*/
    // static lv_color_t buf_3_1[MY_DISP_HOR_RES * MY_DISP_VER_RES];            /*An other screen sized buffer*/
    // lv_disp_draw_buf_init(&draw_buf_dsc_3, buf_3_1, NULL, MY_DISP_VER_RES * 10);   /*Initialize the display buffer*/

    /*-----------------------------------
     * Register the display in LVGL
     * 在LVGL中注册显示设备
     *----------------------------------*/
    static lv_disp_drv_t disp_drv;                         /*Descriptor of a display driver*/
    lv_disp_drv_init(&disp_drv);                    /*Basic initialization*/

    /*Set up the functions to access to your display*/

    /*Set the resolution of the display*/
    disp_drv.hor_res = MY_DISP_HOR_RES;
    disp_drv.ver_res = MY_DISP_VER_RES;

    /*Used to copy the buffer's content to the display*/
    /*用来将缓冲区的内容复制到显示设备*/
    disp_drv.flush_cb = disp_flush;

    /*Set a display buffer*/
    disp_drv.draw_buf = &draw_buf_dsc_1;
    //disp_drv.draw_buf = &draw_buf_dsc_2;
    /*Required for Example 3)*/
    //disp_drv.full_refresh = 1

    /* Fill a memory array with a color if you have GPU.
     * Note that, in lv_conf.h you can enable GPUs that has built-in support in LVGL.
     * But if you have a different GPU you can use with this callback.*/
    //disp_drv.gpu_fill_cb = gpu_fill;

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*Initialize your display and the required peripherals.*/
static void disp_init(void)
{
    /*You code here*/
}

/*Flush the content of the internal buffer the specific area on the display
 *You can use DMA or any hardware acceleration to do this operation in the background but
 *'lv_disp_flush_ready()' has to be called when finished.*/
#include "ugui.h"
typedef uint16_t     UG_U16;
typedef int16_t      UG_S16;
UG_S16 cc = 1;
#include "FT_Log.h"
static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
    /*The most simple case (but also the slowest) to put all pixels to the screen one-by-one*/
    int32_t x;
    int32_t y;
    FT_LogPrint(LOG_INFO, "x1 = %d,y1 = %d\r\n",area->x1,area->y1);
    FT_LogPrint(LOG_INFO, "x2 = %d,y2 = %d\r\n",area->x2,area->y2);
#if 0
    for(y = area->y1; y <= area->y2; y++) {
        for(x = area->x1; x <= area->x2; x++) {
            /*Put a pixel to the display. For example:*/
            /*put_px(x, y, *color_p)*/
            //FT_LogPrint(LOG_INFO, " *color_p = %d\r\n",* color_p);
            UG_DrawPixel(x,y,color_p->full);
            color_p++;
        }
    }
    ft_disp_full_gram();
#else
    ft_disp_full_gram_lv_malloc((unsigned short )area->x1,(unsigned short )area->y1,(unsigned short )area->x2,(unsigned short )area->y2);
    for(y = area->y1; y <= area->y2; y++) {
        for(x = area->x1; x <= area->x2; x++) {
            //_pset(x,y,color_p->full);
            UG_DrawPixel(x,y,color_p->full);
            color_p++;
        }
    }
    ft_disp_full_gram_lv((unsigned short )area->x1,(unsigned short )area->y1,(unsigned short )area->x2,(unsigned short )area->y2);
#endif
    /*IMPORTANT!!!
     *Inform the graphics library that you are ready with the flushing*/
    lv_disp_flush_ready(disp_drv);/*通知图形库，已经刷新完毕了*/
}

/*OPTIONAL: GPU INTERFACE*/

/*If your MCU has hardware accelerator (GPU) then you can use it to fill a memory with a color*/
//static void gpu_fill(lv_disp_drv_t * disp_drv, lv_color_t * dest_buf, lv_coord_t dest_width,
//                    const lv_area_t * fill_area, lv_color_t color)
//{
//    /*It's an example code which should be done by your GPU*/
//    int32_t x, y;
//    dest_buf += dest_width * fill_area->y1; /*Go to the first line*/
//
//    for(y = fill_area->y1; y <= fill_area->y2; y++) {
//        for(x = fill_area->x1; x <= fill_area->x2; x++) {
//            dest_buf[x] = color;
//        }
//        dest_buf+=dest_width;    /*Go to the next line*/
//    }
//}


#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif
